import numpy as np
import scipy.optimize as op
def fun(x):
    a = [1.25,8.75,0.5,5.75,3,7.25]
    b = [1.25,0.75,4.75,5,6.5,7.25]
    X = [5,2]
    Y = [1,7]
    sum = 0
    for i in range(12):
        sum += x[i] * ((X[i//6]-a[i%6])**2 + (Y[i//6]-b[i%6])**2) ** 0.5

    return sum

def con1(x):
    return x[0]+x[6]-3

def con2(x):
    return x[1]+x[7]-5

def con3(x):
    return x[2]+x[8]-4

def con4(x):
    return x[3]+x[9]-7

def con5(x):
    return x[4]+x[10]-6

def con6(x):
    return x[5]+x[11]-11

def con7(x):
    return 20-(x[0]+x[1]+x[2]+x[3]+x[4]+x[5])

def con8(x):
    return 20-(x[6]+x[7]+x[8]+x[9]+x[10]+x[11])

x = np.zeros(12)
bound = (0,None)
bounds = (bound,bound,bound,bound,
          bound,bound,bound,bound,
          bound,bound,bound,bound)
cons1 = {'type':'eq','fun':con1}
cons2 = {'type':'eq','fun':con2}
cons3 = {'type':'eq','fun':con3}
cons4 = {'type':'eq','fun':con4}
cons5 = {'type':'eq','fun':con5}
cons6 = {'type':'eq','fun':con6}
cons7 = {'type':'ineq','fun':con7}
cons8 = {'type':'ineq','fun':con8}

cons = [cons1,cons2,cons3,cons4,
        cons5,cons6,cons7,cons8]

prob = op.minimize(fun,x,method='SLSQP',bounds=bounds,constraints=cons)
x = prob.x
print("最优值为：",fun(x))
print("最优解为：\n")
for i in range(12):
    print(x[i])